package xiaye.mycrawler;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.inject.Inject;

public class SimpleCrawlerThread extends CrawlerThread{
	
	@Inject
	public SimpleCrawlerThread(Frontier frontier, PageStore pageStore){
		super(frontier, pageStore);
	}
	
	@Override
	public void run(){
		try {
			//System.out.println("start url " +address.getURL());
			sleep(new Random().nextInt(1000));
			Page page = new Page();
			page.setFinalAddress(address);
			page.setStartAddress(address);
			List<Address> outlinks = new ArrayList<Address>();
			if (address.toString().length() < 7){
				for (int i = 0; i < 2; i++){
					outlinks.add(new Address(address.toString()+"."+String.valueOf(i)));
				}
			}
			page.setOutLinks(outlinks);
			frontier.processPage(page);
			pageStore.storePage(page);
			//System.out.println("finish url "+address.getURL());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
